package com.ai.notices.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import org.apache.log4j.Logger;

public class HttpClientUtil {
    
    private static final Logger log=Logger.getLogger(HttpClientUtil.class);

    /**
     * 使用POST方法提交http请求，返回服务端响应结果
     * @param targetUrl
     * @param reqMap
     * @return
     * @throws Exception
     */
    public static String sendPostRequest(String targetUrl, String charSet, Map<String,String> reqMap) throws Exception {
        URL url = new URL(targetUrl);
        HttpURLConnection urlConn=null;
        try {
            
            urlConn = (HttpURLConnection) url.openConnection();
            urlConn.setConnectTimeout(10000);
            urlConn.setReadTimeout(10000);
            urlConn.setDoOutput(true);
            urlConn.setDoInput(true);
            urlConn.setUseCaches(false);
            
            urlConn.setRequestMethod(HttpConstants.POST);
            
            OutputStream outStrm = urlConn.getOutputStream();
            
            outStrm.write(buildLinkData(reqMap).getBytes());
            InputStreamReader inStrm = new InputStreamReader(
                    urlConn.getInputStream(), charSet);
            BufferedReader br = new BufferedReader(inStrm);
            String line;
            StringBuilder rspString = new StringBuilder();
            while ((line = br.readLine()) != null) {
                rspString.append(line);
            }
            return rspString.toString();
            
        } catch (Exception e) {
            String errorMsg="post http请求异常。targetUrl："+targetUrl+"；charSet："+charSet+"；reqMap："+reqMap.toString();
            log.error(errorMsg, e);
        }
        return null;
    }
    /**
     * 拼接请求参数字符串
     * @param map
     * @return
     */
    private static String buildLinkData(Map<String, String> map) {
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        for (String key : map.keySet()) {
            if(!isFirst) {
                sb.append('&');
            } else {
                isFirst = false;
            }
            sb.append(key).append('=').append(map.get(key));
        }
        return sb.toString();
    }
    
    /**
     * 使用POST方法提交http请求，返回服务端响应结果
     * @param targetUrl
     * @param reqMap
     * @return
     * @throws Exception
     */
    public static String sendPostRequest(String targetUrl, String charSet,String reqData) throws Exception {
        URL url = new URL(targetUrl);
        HttpURLConnection urlConn=null;
        try {
            
            urlConn = (HttpURLConnection) url.openConnection();
            urlConn.setConnectTimeout(60000);
            urlConn.setReadTimeout(60000);
            urlConn.setDoOutput(true);
            urlConn.setDoInput(true);
            urlConn.setUseCaches(false);
            
            urlConn.setRequestMethod(HttpConstants.POST);
            
            OutputStream outStrm = urlConn.getOutputStream();
            
            outStrm.write(reqData.getBytes());
            InputStreamReader inStrm = new InputStreamReader(
                    urlConn.getInputStream(), charSet);
            BufferedReader br = new BufferedReader(inStrm);
            String line;
            StringBuilder rspString = new StringBuilder();
            while ((line = br.readLine()) != null) {
                rspString.append(line);
            }
            return rspString.toString();
            
        } catch (Exception e) {
            String errorMsg="post http请求异常。targetUrl："+targetUrl+"；charSet："+charSet+"；reqData："+reqData;
            log.error(errorMsg, e);
        }
        return null;
    }
    
}
